var session = sessionStorage.getItem("session");
if (!session) {
	window.parent.location = "../../login.html";
}
$(document).ready(function() {
	$('#tt').tree({
		url: IP + "GeDepartAndUserInfo?callback=?&session=" + session,
		loadFilter:function(rows){
			return convert(rows)
		},
		onClick: function(node) {

			if (node.id > 4999) {
				$.messager.alert('提醒', "你选的不是管理员，请重新选择", 'info');
				return
			} else { //获取管理员的角色；
				$('#UserId').attr("value", node.id);
				$("#roleList").trigger("reloadGrid");
				$.ajax({
					url: IP + "GetSevRoles?callback=?&session=" + session + "&UserId=" + node.id,
					type: "GET",
					dataType: "jsonp",
					success: function(data) {	 
						$.each(data, function(index, item) {
								if (item.checked == 1) {
									$("#roleList").jqGrid("setSelection", (index + 1));
								}
							})  
					}
				})

			}
		}
	});

	$("#roleList").jqGrid({
		url: IP + "GetSevRoles?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			name: 'rid',
			label: '序号',
			width: 50,
			hidden:true,
		}, {
			name: 'rname',
			label: '角色',
			width: 200
		}],
		multiselect: true,
		multiboxonly: true,
		loadonce: true,
		viewrecords: true,
		//rownumbers:true,
		rowNum: 15,
		autowidth:true,
		page: 1,
		loadtext: '正在加载数据',
		pager: "#roleListPager",
	})
	var h = $("body").height();
	$("#roleList").jqGrid("setGridHeight",h-120)
});
 
function serchRoles() {
	var content = $("#searchRole").val();
		$("#roleList").jqGrid('filterInput',content);
}



function SaveRole() {
	var UserId = $("#UserId").val();
	if (!UserId) {
		$.messager.alert("提醒", "你还没有选择用户", 'info');
		return false
	}
	var checkeds = $("#roleList").jqGrid("getGridParam", "selarrrow");
	var rids = [];
	for (var i = 0; i < checkeds.length; i++) {
		var rid = $("#roleList").getCell(checkeds[i], "rid");
		rids.push(rid);
	}
	var savestr = rids.join(",");
	if (!savestr) {
		$.messager.alert("提醒", '角色不能为空', 'info');
		return false
	} else {
		$.ajax({
			url: IP + "UpdateUserRoles?callback=?",
			type: 'post',
			dataType: "jsonp",
			jsonp: "callback",
			jsonpCallback: "resultHandler",
			data: {
				session: session,
				UserId: UserId,
				savestr: savestr,
			},
			success: function(json) {
				if (json.result == 200) {
				insertopertlog('系统管理','角色管理','编辑','','');
					$.messager.show({
						title: '提醒',
						msg: '角色编辑成功',
						timeout: 1300,
						showType: 'slide',
						style: {
							right: '',
							bottom: ''
						}
					});
				} else {
					$.messager.show({
						title: '提醒',
						msg: '角色编辑失败',
						timeout: 1300,
						showType: 'slide',
						style: {
							right: '',
							bottom: ''
						}
					});
				}
			},
			
			error: function() {
				$.messager.show({
					title: '提醒',
					msg: '角色编辑失败',
					timeout: 1300,
					showType: 'slide',
					style: {
						right: '',
						bottom: ''
					}
				});
			}

		})
	}
	addrow()
}
function convert(json) {
	var json1={"pid":0,"id":1,"text":"县市","state":true,"checked":false};
	    json1.children= json;
	var json2 =[];
	    json2.push(json1);
	   // console.log(json2);
	return json2;
}

function addrow(){
	$("#roleList").trigger("reloadGrid");
	$("#tt").tree("reload");
}
